'''
将 diff ZipArchive 汇集到 images ZipArchive
'''
# dates = ['0729','0730','0731','0801','0804',]
# dates = ['0813']
# dates = [
#     # '0808',
#     # '0811','0812','0813','0814','0815',
#     # '0818','0819','0820',
#     # '0821','0822'
#     # '0825','0826','0827','0828','0829',
#     # '0901','0902','0903','0904','0905',
#     # '0908','0909','0910','0911',
#     '0912',
#     '0915','0916','0917',
#     ]
# categories = ['paike' , 'sweater' , 'leather']
# categories = ['sweater' , 'leather']
# categories = ['leather']

# dates = ['0916']
# categories = ['paike']

# dates = ['0916', '0917']
# categories = ['trench']

# dates = ['0916']
# categories = ['leafur']

# dates = ['0917', '0918', '0919', '0922', '0923', '0924']
# categories = ['slipdress']

# dates = ['0917', '0918', '0919', '0922', '0923', '0924', '0925', '0926', '0928', '0929', '0930']
dates = ['1009', '1010', '1011', 
        '1013', '1014', 
        '1016', 
        '1020', '1021', '1022',]
categories = ['dress']

dates = ['0917','0918','0919',
        '0922','0923','0924',]
categories = ['feather']

dates = ['1020', '1021','1022', '1023', '1024',
        '1027', '1028']
categories = ['suit',]

dates = ['1020', '1021',]
categories = ['suitset',]

from tqdm import tqdm
import os,shutil
from util_for_os import ose,osj

dir = '/mnt/nas/datasets/diction'
IMAGES_DIR = lambda category : osj( dir , f'images_{category}' )
ZIPArchive_DIR = lambda category: osj( dir , f'ZipArchive_{category}' )

get_clo_diff = lambda category,date : osj( dir , f'{category}{date}_img_clo_diff' )
get_zip = lambda category,date : osj( dir , f'{category}{date}' )


# 先确认路径存在
for category in categories:
    
    for date in dates:


        if date >= '0826' and category == 'leather':
            continue

        clo_diff_dir = get_clo_diff( category , date )
        zip_dir = get_zip( category , date )

        diff_names_path = osj( clo_diff_dir , 'names.txt' )
        zip_names_path = osj( zip_dir , 'names.txt' )


        assert ose( clo_diff_dir ),clo_diff_dir
        assert ose( zip_dir ),zip_dir
        assert ose( diff_names_path ),diff_names_path
        assert ose(zip_names_path),zip_names_path


    assert ose( ZIPArchive_DIR(category) )
    assert ose( IMAGES_DIR(category) )

# import pdb
# pdb.set_trace()

for category in categories:
    
    clo_diff_dirs = []
    zip_dirs = []

    clo_diff_names_paths = []
    zip_names_paths = []
    
    for date in dates:


        if date >= '0826' and category == 'leather':
            continue

        clo_diff_dir = get_clo_diff( category , date )
        zip_dir = get_zip( category , date )

        diff_names_path = osj( clo_diff_dir , 'names.txt' )
        zip_names_path = osj( zip_dir , 'names.txt' )


        assert ose( clo_diff_dir ),clo_diff_dir
        assert ose( zip_dir ),zip_dir
        assert ose( diff_names_path ),diff_names_path
        assert ose(zip_names_path),zip_names_path

        clo_diff_dirs.append(clo_diff_dir)
        zip_dirs.append(zip_dir)
        
        clo_diff_names_paths.append(diff_names_path)
        zip_names_paths.append(zip_names_path)


    assert ose( ZIPArchive_DIR(category) )
    assert ose( IMAGES_DIR(category) )
    

    for clo_diff_dir,clo_diff_names_path,zip_dir,zip_names_path in zip(
        clo_diff_dirs,clo_diff_names_paths,zip_dirs,zip_names_paths
    ):
        with open( clo_diff_names_path , encoding='utf-8' ) as f:
            clo_names = f.readlines()
        record_clo_num = 0
        for clo_name in tqdm(clo_names):
            clo_name = clo_name.strip()
            if not clo_name.endswith( '.jpg' ) : continue
            if not ose(osj( clo_diff_dir , clo_name )):
                print( '不存在', osj( clo_diff_dir , clo_name ) )
                continue
            shutil.copy2( osj( clo_diff_dir , clo_name ) , IMAGES_DIR(category) )
            record_clo_num += 1

        with open( zip_names_path , encoding='utf-8' ) as f:
            zip_names = f.readlines()
        record_zip_num = 0
        for zip_name in tqdm(zip_names):
            zip_name = zip_name.strip()
            if not zip_name.endswith( '.zip' ) : continue
            if not ose(osj( zip_dir , zip_name )):
                print( '不存在', osj( zip_dir , zip_name ) )
                continue
            shutil.copy2( osj( zip_dir , zip_name ) , ZIPArchive_DIR(category) )
            record_zip_num += 1
        
        print(clo_diff_dir,' >>')
        print( f'\tclo ratio : {record_clo_num}/{len(clo_names)}' )
        print( f'\tzip ratio : {record_zip_num}/{len(zip_names)}' )